/**
 * @author Tobias Boehm
 *
 * 08.06.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.client.preparematch.ClientStartMatchRequestMessage;
import org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage;

/**
 * This is a server message you should use to reply to an {@link ClientStartMatchRequestMessage}.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerMatchpreprataionStartRequestReplyMessage extends
		AbsServerMatchMessage implements ICopyable<ServerMatchpreprataionStartRequestReplyMessage> {

	public static enum StartReplyReason{OK,NO_PERMISSION,PLAYER_NOT_READY};
	
	private boolean mAccepted = false;
	private StartReplyReason mReason = StartReplyReason.NO_PERMISSION;
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCHPREPARATION_START_REQUEST_REPLY;
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage#onReadTransmissonMatchData(java.io.DataInputStream)
	 */
	@Override
	protected void onReadTransmissonMatchData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mAccepted = pDataInputStream.readBoolean();
		this.mReason = StartReplyReason.valueOf(pDataInputStream.readUTF());
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage#onWriteTransmissonMatchData(java.io.DataOutputStream)
	 */
	@Override
	protected void onWriteTransmissonMatchData(
			DataOutputStream pDataOutputStream) throws IOException {
	
		pDataOutputStream.writeBoolean(this.mAccepted);
		pDataOutputStream.writeUTF(this.mReason.name());
	}

	/**
	 * @return the mAccepted
	 */
	public boolean isAccepted() {
		return mAccepted;
	}

	/**
	 * @param mAccepted the mAccepted to set
	 */
	public void setAccepted(boolean mAccepted) {
		this.mAccepted = mAccepted;
	}

	/**
	 * @return the mReason
	 */
	public StartReplyReason getReason() {
		return mReason;
	}

	/**
	 * @param mReason the mReason to set
	 */
	public void setReason(StartReplyReason mReason) {
		this.mReason = mReason;
	}

	@Override
	public ServerMatchpreprataionStartRequestReplyMessage copy() {
		
		final ServerMatchpreprataionStartRequestReplyMessage copy = new ServerMatchpreprataionStartRequestReplyMessage();
		
		copy.mAccepted = this.mAccepted;
		copy.mMatchID = this.mMatchID;
		copy.mReason = this.mReason;
		
		return copy;
	}
}
